import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/common_model/notice_list_model.dart';
import 'package:yysk_app/model/market_model/market_incubation_detail_model.dart';
import 'package:yysk_app/net/api/common_api.dart';
import 'package:yysk_app/net/api/market_url.dart';
import 'package:yysk_app/net/http.dart';

class MarketCollectionDetailController extends GetxController {
  final detail = Rx([]);
  final title = '孵化详情'.obs;
  var id = '';
  final marketDetail = Rx<MarketIncubationDetailModel?>(null);
  final currentTab = 'detail'.obs;
  final ScrollController scrollController = ScrollController();
  final listnotice = RxList<dynamic>([]);

    //相关公告
  int pageNum = 1;
  final noticeList = RxList<NoticeListItem>();
  @override
  // ignore: unnecessary_overrides
  void onInit() {
    super.onInit();
    id = Get.parameters['id'] ?? Get.arguments['id'] ?? '0';
    getDetail().then((res){
        getNoticeList();
    });
  
  }

  @override
  // ignore: unnecessary_overrides
  void onClose() {
    super.onClose();
  }
    Future<void> handleRefresh() async {
          //  getList(loadMore: false);

    return;
  }
    Future<void> getDetail({loadMore = false}) {
    return http.netFetch<MarketIncubationDetailModel>(
      MarketUrl.getCollectionDetail,
      method: Method.post,
      params: {'collectionId': id},
      success: (result) {
        marketDetail.value = result.data;
   
      },
    );
  }



      //获取notice公告数据
  Future<void> getNoticeList({loadMore = false,id=0}) {

    if (loadMore) {
      pageNum++;
    } else {
      pageNum = 1;
    }

    try {
      return http.netFetch<NoticeListModel>(
        CommonApi.noticeList,
        method: Method.post,
        params: {'pageNum': pageNum, 'pageSize': 100,"collectionId": marketDetail.value?.collectionId},
        success: (result) {
          List<NoticeListItem> items = result.data?.list ?? [];
   
          if (loadMore && items.isEmpty) {
            pageNum--;
          }
          if (!loadMore) {
            noticeList.clear();
          }
          noticeList.addAll(items);
        },
      );
    } catch (e) {
      debugPrint(e.toString());
      if (loadMore) {
        pageNum--;
      }
      return Future.error(e);
    }
  }

}
